/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 signal.cpp

	$Header: /heroes4/signal.cpp $

	$NoKeywords: $

 ************************************************************************/
#include "precompiled.h"
#include "signal.h"

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#pragma warning( disable: 4786 )


#define WIN32_LEAN_AND_MEAN
// as opposed to fat and bloated?
#define NOMINMAX
#include <windows.h>

struct t_signal::t_data
{
	HANDLE event;
};

// --------------------------------------------------------------
// wrapper for Windows Event object.  Used for inter-thread or
// inter-process communication
// --------------------------------------------------------------
t_signal::t_signal() : m_data( new t_data )
{
	m_data->event = CreateEvent( 0, false, false, NULL );
}

// --------------------------------------------------------------
// wrapper for Windows Event object.  Used for inter-thread or
// inter-process communication
// --------------------------------------------------------------
t_signal::~t_signal()
{
	CloseHandle( m_data->event );
}

// --------------------------------------------------------------
// wrapper for Windows Event object.  Used for inter-thread or
// inter-process communication
// --------------------------------------------------------------
void t_signal::set()
{
	SetEvent( m_data->event );
}

// --------------------------------------------------------------
// wrapper for Windows Event object.  Used for inter-thread or
// inter-process communication
// --------------------------------------------------------------
bool t_signal::wait()
{
	DWORD result = WaitForSingleObject( m_data->event, INFINITE );

	return result == WAIT_OBJECT_0;
}

// --------------------------------------------------------------
// wrapper for Windows Event object.  Used for inter-thread or
// inter-process communication
// --------------------------------------------------------------
bool t_signal::wait( int delay )
{
	DWORD result = WaitForSingleObject( m_data->event, delay );

	return result == WAIT_OBJECT_0;
}

// --------------------------------------------------------------
// wrapper for Windows Event object.  Used for inter-thread or
// inter-process communication
// --------------------------------------------------------------
bool t_signal::wait_for_message( int delay )
{
	MSG msg;

	if (PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE ))
		return false;

	DWORD result = MsgWaitForMultipleObjects( 1, &m_data->event, false, delay, 
											  QS_ALLEVENTS );

	return result == WAIT_OBJECT_0;
}
